<template>
  <ele-modal
    form
    :width="660"
    :model-value="modelValue"
    title="添加推送用户"
    @update:modelValue="updateModelValue"
  >
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        cache-key="systemUserTable"
        border
        stripe
      >
        <template #action="{ row }">
          <el-link type="primary" :underline="false" @click="add(row)">
            添加
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
  </ele-modal>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './components/search.vue';
  import { pageStudent } from '@/api/student';
  import { addMpMessageUser } from '@/api/mall';
  import { EleMessage } from 'ele-admin-plus/es';

  const emit = defineEmits(['done', 'update:modelValue']);

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      width: 85,
      align: 'center',
      label: 'ID'
    },
    {
      prop: 'realname',
      label: '姓名',
      width: 100
    },
    {
      prop: 'tel',
      label: '电话',
      width: 140
    },

    {
      columnKey: 'action',
      label: '操作',
      slot: 'action'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 添加到推送名单 */
  const add = (row) => {
    addMpMessageUser({ member_id: row.id })
      .then(() => {
        EleMessage.success('操作成功!');
      })
      .catch(() => {});
  };

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageStudent({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };
</script>
